<?php
  $_SESSION['installer'] = true;
  $_SESSION['admin_salt'] = substr(md5(rand()),0,16);
  $_SESSION['real_admin_pass'] = $_SESSION['admin_pass'];
  $_SESSION['admin_pass'] = md5(sha1($_SESSION['admin_pass']));
  if(!is_email($_SESSION['admin_email'])){
    $_SESSION['errors'] = L_EMAIL_NOT_VALID;
  } else {
    unset($_SESSION['errors']);
    $link = mysql_connect($_SESSION['db_host'],$_SESSION['db_user'],$_SESSION['db_pass']) or header("Location: install.php?step=db");
    mysql_select_db($_SESSION['db_name']);
    $sql = replace_session(file_get_contents('db.bak'));
    $queries = explode(";", $sql);
    foreach($queries as $query){
      mysql_query($query,$link);
    }
    unset($_SESSION['errors']);
    $config = replace_session(file_get_contents('conf'));
    if(!file_exists('../conf/db.php')){
      $db_file = fopen('../conf/db.php', 'w+');
      fwrite($db_file, $config);
      fclose($db_file);
    }
    //Patch for admin password
    $pass = $_SESSION['admin_pass'];
    $salt = substr(md5(rand()),0,16);
    $user = $_SESSION['admin_user'];
    mysql_query("UPDATE `kleo_users` SET `pass` =  '$pass',
`salt` = '$salt', `username` = '$user' WHERE `uid` =1;");
    header('Location: install.php?step=finish');
  }
?>
